﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for validCardBUS
/// </summary>
public class validCardBUS
{

    public validCardBUS()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public Card CheckCC(string CardNumber)
    {
        Card ri = new Card();
        ri.CardValid = false;


        byte[] number = new byte[16];


        int len = 0;
        for (int i = 0; i < CardNumber.Length; i++)
        {
            if (char.IsDigit(CardNumber, i))
            {
                if (len == 16) return ri;
                number[len++] = byte.Parse(CardNumber[i].ToString());
            }
        }


        int sum = 0;
        for (int i = len - 1; i >= 0; i--)
        {
            if (i % 2 == len % 2)
            {
                int n = number[i] * 2;
                sum += (n / 10) + (n % 10);
            }
            else
                sum += number[i];
        }

        ri.CardValid = (bool)(sum % 10 == 0);
        if ((ri.CardValid == true))
        {
            switch (CardNumber.Substring(0, 1))
            {

                case "4":
                    ri.CardType = "VISA";
                    break;
                case "5":
                    ri.CardType = "MasterCard";
                    break;
                default:
                    ri.CardType = "Error";
                    break;
            }
        }
        else
        {
            ri.CardType = "NONE";
        }
        return ri;
    }
}